package com.mail.server.service;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.api.PmsProductAttributeApi;
import com.mail.model.db.PmsProductAttribute;
import com.mail.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Service
public class PmsProductAttributeService {

    @DubboReference
    private PmsProductAttributeApi productAttributeApi;

    // 根据商品分类的id获取商品属性及属性分类
    public List<Map<String,Integer>> queryProductAttributeAndCategory(Integer productCategoryId) {
        List<Map<String,Integer>> mapList = productAttributeApi.queryProductAttributeAndCategory(productCategoryId);
        return mapList;
    }

    // 添加商品属性信息
    public void createProductAttribute(PmsProductAttribute productAttribute) {
        productAttributeApi.createProductAttribute(productAttribute);
    }

    // 批量删除商品属性
    public void deleteAttributeByIds(List<Integer> ids) {
        // 将int集合转换为long集合
        List<Long> deleteIds = JSONArray.parseArray(ids.toString(),Long.class);
        productAttributeApi.deleteAttributeByIds(deleteIds);
    }

    // 根据分类查询属性列表或参数列表
    public PageResult queryAttributeList(Integer cid, Integer type, Integer pageNum, Integer pageSize) {
        Page<PmsProductAttribute> page = productAttributeApi.queryAttributeList(cid,type,pageNum,pageSize);
        PageResult pageResult = new PageResult();
        pageResult.setList(page.getRecords());
        pageResult.setPageNum(page.getCurrent());
        pageResult.setPageSize(page.getSize());
        pageResult.setTotalPage(page.getPages());
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    // 修改商品属性信息
    public void updateProductAttribute(Long id, PmsProductAttribute productAttributeParam) {
        productAttributeApi.updateProductAttribute(id,productAttributeParam);
    }

    // 查询单个商品属性
    public PmsProductAttribute queryProductAttributeById(Long id) {
        PmsProductAttribute productAttribute = productAttributeApi.queryProductAttributeById(id);
        return productAttribute;
    }
}
